package j13;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author chengshuai
 * @date 2021/5/5 20:13
 */
public class AwaitSignalDemo {

    static Lock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static void main(String[] args) {
        new Thread(()->{
            ThreadUtil.sleep(3000);
            System.out.println(Thread.currentThread().getName() + "\t" + "----come in");
            lock.lock();
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + "\t" + "----执行结束");
            }
        },"A").start();

        new Thread(()->{
            lock.lock();
            condition.signal();
            System.out.println(Thread.currentThread().getName() + "\t" + "----执行结束");
            lock.unlock();
        },"B").start();
    }

}
